﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebLibSolPedBE;

namespace WebApplication1
{
    public partial class formDocSOCompra : System.Web.UI.Page
    {
        List<DocSOCompraBE> lst = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["nombreApp"] = "docSOC";
                if (Session["objUser"] == null)
                {
                    Response.Redirect("credencial.aspx");
                }
                else
                {
                    ModalPopupExtender1.Hide();
                    CargarLista();
                }
            }
            else
            {
                string acci = Session["accion"] as string;
                if (acci.Equals("R"))
                {
                    CargarLista();
                    Session["accion"] = "";
                }
            }
        }

        private void CargarLista()
        {
            List<DocSOCompraBE> lst = LibSolPedDALF.DaoFactory.DocSOCompraF.getLstDocSOCompra();
            gvGrid.DataSource = lst;
            gvGrid.DataBind();
            Session["lstDocSOC"] = lst;
        }

        protected void gvGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                GridView gv = (GridView)sender;
                int filaSeleccionada = Convert.ToInt32(e.CommandArgument);
                lst = (List<DocSOCompraBE>)Session["lstDocSOC"];
                try
                {
                    Session.Add("objDocSOC", lst[filaSeleccionada]);
                }
                catch (Exception ex) { }

                string url = String.Format("formDocSOCompraDet.aspx");
                Response.Redirect(url);
            }
        }

        protected void gvGrid_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Retrieve the LinkButton control from the first column.
                LinkButton addButton = (LinkButton)e.Row.Cells[2].Controls[2].FindControl("LinkButton1det");
                // Set the LinkButton's CommandArgument property with the
                // row's index.
                addButton.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find the control
                var lblBelnrBudat = (Label)e.Row.FindControl("lblBelnrBudat");
                var lblBlart = (Label)e.Row.FindControl("lblBlart");
                var lblLifnr = (Label)e.Row.FindControl("lblLifnr");
                var lblWrbtrWaers = (Label)e.Row.FindControl("lblWrbtrWaers");
                //Display the data
                var data = e.Row.DataItem;
                DocSOCompraBE it = (DocSOCompraBE)data;
                lblBelnrBudat.Text = it.BELNR + " " + it.BUDAT;
                lblBlart.Text = it.DBLART;
                lblLifnr.Text = it.DLIFNR;
                lblWrbtrWaers.Text = it.WRBTR + " " + it.WAERS;
            }
        }

        protected void gvGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView gv = (GridView)sender;
            int filaSeleccionada = gv.SelectedIndex;
            lst = (List<DocSOCompraBE>)Session["lstDocSOC"];
            try
            {
                Session.Add("objDocSOC", lst[filaSeleccionada]);
                Response.Redirect("formDocSOCompraDet.aspx", false);
            }
            catch (Exception ex)
            {

            }
        }

        protected void LinkButton1det_Click(object sender, EventArgs e)
        {

        }
    }
}